package blockchain

import (
	"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
	//"fmt"
)

//
// sdk配置及调用链码封装
//

// sdk相关配置
var (
	SDK             *fabsdk.FabricSDK                                                                  // Fabric提供的SDK
	ChannelName     = ""                                                                     // 通道名称
	ChaincodeName   = ""                                                             // 链码名称
	Org             = ""                                                                           // 组织名称
	User            = ""                                                                          // 用户
	ConfigPath      = "" // 配置文件路径
	TargetEndpoints = ""
)

// sdk初始化
func Init() {

}

// 区块链交互
func ChannelExecute(fcn string, args [][]byte){

}

// 区块链查询
func ChannelQuery(fcn string, args [][]byte) {

}

//"github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
//func UserEnroll(arg UserImpl) error {
//	user := msp.User{
//		/*
//			id                    string
//			mspID                 string
//			enrollmentCertificate []byte
//			privateKey            core.Key
//		*/
//	}
//	context := mocks.NewMockContext(&user)
//
//	//endpointConfig:=fab.EndpointConfig{}
//	//peer := fab.PeerConfig{}
//	//peer.URL = "127.0.0.1"
//"github.com/hyperledger/fabric-sdk-go/pkg/msp"
//	//endpointConfig, err := fab.BuildConfigEndpointFromOptions(&endpointConfig)
//
//	configBackend, err := SDK.Config()
//
//	config1, err := fab.ConfigFromBackend(configBackend)
//	if err != nil {
//		fmt.Println("获取配置出错Failed to get endpoint config from backend")
//	}
//	endpointConfig := config1.(*fab.EndpointConfig)
//
//	if err != nil {
//		fmt.Println("设置配置文件出错:", err)
//		return err
//	}
//
//	context.SetEndpointConfig(endpointConfig)
//
//	//
//	netConfig := context.EndpointConfig().NetworkConfig()
//
//	orgConfig, ok := netConfig.Organizations[Org]
//
//	fmt.Println(orgConfig, ok)
//	//
//
//	caClient, err := msp.NewCAClient(Org, context)
//
//	if err != nil {
//		fmt.Println("创建caClient错误:", err)
//		return err
//	}
//
//	enrollReq := api.EnrollmentRequest{
//		// The identity name to enroll
//		Name: arg.Name,
//		// The secret returned via Register
//		Secret: arg.Secret,
//		// AttrReqs are requests for attributes to add to the certificate.
//		// Each attribute is added only if the requestor owns the attribute.
//		//AttrReqs []*AttributeRequest
//		// Profile is the name of the signing profile to use in issuing the X509 certificate
//		Profile: arg.Profile,
//		// Label is the label to use in HSM operations
//		Label: arg.Lable,
//		// The type of the enrollment request: x509 or idemix
//		// The default is a request for an X509 enrollment certificate
//		Type: arg.Type,
//	}
//	err = caClient.Enroll(&enrollReq)
//	if err != nil {
//		fmt.Println("enroll错误:", err)
//		return err
//	}
//	return nil
//
//}
//
//func EnrollUser(username string, password string) (bool, error) {
//	ctx := SDK.Context()
//	mspClient, err := msp.New(ctx)
//	if err != nil {
//		fmt.Printf("Failed to create msp client: %s\n", err)
//		return true, err
//	}
//
//	_, err = mspClient.GetSigningIdentity(username)
//	if err == msp.ErrUserNotFound {
//		fmt.Println("Going to enroll user")
//		err = mspClient.Enroll(username, msp.WithSecret(password))
//		if err != nil {
//			fmt.Printf("Failed to enroll user: %s\n", err)
//			return true, err
//		}
//		fmt.Printf("Success enroll user: %s\n", username)
//	} else if err != nil {
//		fmt.Printf("Failed to get user: %s\n", err)
//		return false, err
//	}
//	fmt.Printf("User %s already enrolled, skip enrollment.\n", username)
//	return true, nil
//}
//

////Register a new user with username , password and department.
//func RegisterlUser(username, password, department string) error {
//	ctx := SDK.Context()
//	mspClient, err := msp.New(ctx)
//	if err != nil {
//		fmt.Printf("Failed to create msp client: %s\n", err)
//	}
//	request := &msp.RegistrationRequest{
//		Name:        username,
//		Type:        "user",
//		Affiliation: department,
//		Secret:      password,
//	}
//
//	secret, err := mspClient.Register(request)
//	if err != nil {
//		fmt.Printf("register %s [%s]\n", username, err)
//		return err
//	}
//	fmt.Printf("register %s successfully,with password %s\n", username, secret)
//	return nil
//}
type UserImpl struct {
	Name    string
	Secret  string
	Profile string
	Lable   string
	Type    string
}
